package com.wsu.search.tools;

/**
 * <p>Title: this software is designed by wangsu </p>
 * <p>Description: 公司基本工具类</p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: pacific</p>
 * @author 王谡
 * @version $Header: /CVSRoot/shop/src/com/pacific/basic/util/GetUrl.java,v 1.3 2009/10/26 00:52:54 jjzhang Exp $
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class GetUrl {
  public GetUrl() {

  }
  
  public GetUrl(String proxyHost, String proxyPort) {
    Properties props = new Properties();
    props.put("http.proxyHost", proxyHost);
    props.put("http.proxyPort", proxyPort);
  }

  /**@deprecated
   * 通过代理服务器方式连接
   * @param proxyHost
   * @param proxyPort
   * @param urlStr
   * @return
   */
  public String getHtml(String proxyHost, String proxyPort, String urlStr) {
    String returnStr = "";
    String currentLine = "";
    if (!proxyHost.equals("") && !proxyPort.equals("")) {
      Properties props = new Properties();
      props.put("http.proxyHost", proxyHost);
      props.put("http.proxyPort", proxyPort);
    }
    try {
      URL url = new URL(urlStr);
      URLConnection urlConn = (URLConnection) url.openConnection();
      urlConn.connect();

      InputStream is = urlConn.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      while ( (currentLine = br.readLine()) != null) {
        returnStr += currentLine;
      }
      br.close();
      is.close();
    }
    catch (Exception e) {
      System.out.println("GetUrl:" + e);
    }
    return returnStr;
  }

  /**
   * 不需要代理服务器
   * @param url
   * @return
   */
  public String getHtml(String urlStr) {
    String returnStr = "";
    String currentLine = "";
    try {
      URL url = new URL(urlStr);
      URLConnection urlConn = (URLConnection) url.openConnection();
      urlConn.connect();
      InputStream is = urlConn.getInputStream();
      BufferedReader br = new BufferedReader(new InputStreamReader(is));
      while ( (currentLine = br.readLine()) != null) {
        returnStr += currentLine;
      }
      br.close();
      is.close();
    }
    catch (Exception e) {
      //System.out.println("GetUrl:此地址--"+urlStr+" " + e);
    }
    return returnStr;

  }

  /**@deprecated
   * 保存指定的URL文件
   * @param proxyHost
   * @param proxyPort
   * @param urlStr 保存的URL
   * @param filePath 保存到路径
   * @return
   */
  public boolean saveUrlFile(String proxyHost, String proxyPort, String urlStr,
                             String filePath) {
    boolean bl = false;
    if (!proxyHost.equals("") && !proxyPort.equals("")) {
      Properties props = new Properties();
      props.put("http.proxyHost", proxyHost);
      props.put("http.proxyPort", proxyPort);
    }
    try {
      URL url = new URL(urlStr);
      URLConnection urlConn = (URLConnection) url.openConnection();

      //获得文件名称
      String fileName = url.getFile();
      String[] tempStrArr = fileName.split("/");
      fileName = tempStrArr[tempStrArr.length - 1];
      urlConn.connect();

      InputStream fis = urlConn.getInputStream();
      FileOutputStream fos = new FileOutputStream(new File(filePath, fileName));
      byte[] byteArr = new byte[2048];
      int byteLen = 0;
      while ( (byteLen = fis.read(byteArr, 0, byteArr.length)) != -1) {
        fos.write(byteArr, 0, byteLen);
      }
      fos.close();
      fis.close();
      bl = true;
    }
    catch (Exception e) {
      System.out.println("GetUrl:" + e);
    }
    return bl;
  }
  /**
   * 保存指定URL文件到指定的路径，其中文件名称是跟URL所指的文件文件一样
   * @param urlStr
   * @param filePath
   * @return
   */
  public boolean saveUrlFile(String urlStr, String filePath) {
    boolean bl = false;
    try {
      URL url = new URL(urlStr);
      //获得文件名称
     String fileName = url.getFile();
     String[] tempStrArr = fileName.split("/");
     fileName = tempStrArr[tempStrArr.length - 1];
      URLConnection urlConn = (URLConnection) url.openConnection();
      urlConn.connect();
      InputStream fis = urlConn.getInputStream();
      FileOutputStream fos = new FileOutputStream(new File(filePath, fileName));
      byte[] byteArr = new byte[2048];
      int byteLen = 0;
      while ( (byteLen = fis.read(byteArr, 0, byteArr.length)) != -1) {
        fos.write(byteArr, 0, byteLen);
      }
      fos.close();
      fis.close();
      bl = true;
    }
    catch (Exception e) {
       System.out.println("GetUrl :无法保存此地址--"+urlStr+" " + e);
    }
    return bl;
  }

  /**
   * 写日期
   * @param fileStr
   * @param logStr
   * @return
   */
  public boolean writeLog(String fileStr, String logStr) {
    try {
      FileWriter fw = new FileWriter(new File(fileStr), true);
      java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
          "yyyy-MM-dd HH:mm:ss");

      fw.write(sdf.format(new java.util.Date()));
      fw.write("    " + logStr);
      fw.write("\n");
      fw.close();
    }
    catch (Exception e) {
      System.out.println("Write Log error:" + e);
    }
    return true;
  }

  public static void main(String[] args) {
//    boolean bl = false;
   GetUrl getUrl1 = new GetUrl();
//    if (args != null && args.length > 0) {
//      bl = true;
//    }
//    else {
//      System.out.println("args Error:");
//      System.out.println(
//          "Usage:java GetUrl http://www.law-star.com d:/test/test.txt");
//      System.out.println(
//          "Usage:java GetUrl http://www.law-star.com 192.168.0.60 80  d:/test/test.txt");
//      getUrl1.saveUrlFile("192.168.0.60", "80",
//          "http://ljhaaa.cn.alibaba.com/athena/companydetail/ljhaaa.html",
//                          "d:/test");
//
//    }
//    if (bl) {
//
//      if (args.length < 4) {
//        getUrl1.writeLog(args[1],
//                         String.valueOf(getUrl1.getHtml(args[0]).length()));
//      }
//      else {
//        getUrl1.writeLog(args[3],
//                         String.valueOf(getUrl1.getHtml(args[1], args[2],
//            args[0]).length()));
//      }
//      System.out.println("Write Log Ok!");
//    }
    System.out.println(getUrl1.getHtml("http://www.findchinalaws.com/html/lawsearch.htm"));
  }

}
